﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using PenoyShop.Utilities;
using System.Text;
using PeonyShop.Bussiness;

namespace PeonyShop.Admin
{
    /// <summary>
    /// Summary description for FileHandler
    /// </summary>
    public class FileHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            bool isInsert = false; ;
            try
            {
                string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                string sName = RandomString(10);

                HttpPostedFile file = context.Request.Files["Filedata"];
                if (file.ContentLength > 0)
                {
                    string targetDirectory = context.Server.MapPath(context.Request["folder"]);
                    sName = RandomString(10);
                    string targetFilePath = Path.Combine(targetDirectory, sName + strExtension);
                    file.SaveAs(targetFilePath);
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(sName);
                }
                else
                    Logger.FrameworkLogger.Error("Can not upload file (FileHandler.ashx line 27) " + sName);                

                string sFileName = HttpContext.Current.Request.Form["name"];
                string sDesc = HttpContext.Current.Request.Form["desc"];
                string sMainCate = HttpContext.Current.Request.Form["maincate"];
                string sSubcate = HttpContext.Current.Request.Form["subcate"];
                string iProductId = HttpContext.Current.Request.Form["productid"];
                string IsShowHome = HttpContext.Current.Request.Form["showhomepage"];
                string IsShowOwn = HttpContext.Current.Request.Form["showownpage"];
                string IsActive = HttpContext.Current.Request.Form["IsActive"];
                string IsDelete = HttpContext.Current.Request.Form["IsDelete"];

                //Insert into Database

                BImage bImg = new BImage();
                isInsert = bImg.InsertImage(sFileName, sDesc, sName + strExtension, Convert.ToInt32(sMainCate), Convert.ToInt32(sSubcate), Convert.ToInt32(iProductId), 0, DateTime.Now, 1, Convert.ToBoolean(IsShowHome), Convert.ToBoolean(IsShowOwn), Convert.ToBoolean(IsActive), Convert.ToBoolean(IsDelete));


            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Insert images ",ex);
            }

           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }
    }
}